﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.BandedGrid;
using MSoft.Shipping.Control;
using COMOSOFT.WareHouse_Management.Forms;
using COMOSOFT.WareHouse_Management.Class;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmProductionOutlinePlanningDaily : Form
    {
        COMOSOFT.Production.Forms.FrmPreView frmPV = null;
        List<object> ListItem=new List<object>();
        string strLine;           
        ClassShare cls = new ClassShare("FrmProductionOutlinePlanningDaily");

        int Year, Month, OID, SubIID, SubFLID,Line;
        DataTable dtb;
        public FrmProductionOutlinePlanningDaily(object _Year, object _Month, object _OID, object _SubIID, object _SubFLID)
        {
            InitializeComponent();
            this.Year =Convert.ToInt32(_Year);
            this.Month = Convert.ToInt32(_Month);
            this.OID = Convert.ToInt32(_OID);
            this.SubIID = Convert.ToInt32(_SubIID);
            this.SubFLID = Convert.ToInt32(_SubFLID);
        }   
    
        #region[Load Data]
        public void LoadData()
        {
            try
            {
                BandedGridView view = gridControl_ProducionOP.MainView as BandedGridView;
                gridBand_Qty.Columns.Clear();
                string Days = null;
                Days=GetDays(Year,Month);
                DataTable dtb = cls.GetData("sp_ProductionFollow", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Days" }, new object[] { Year, Month, OID, SubIID, SubFLID,Days});
                for (int i = 26; i < dtb.Columns.Count; i++)
                {
                    BandedGridColumn colQty = view.Columns.AddVisible(dtb.Columns[i].ColumnName) as BandedGridColumn;
                    colQty.Caption = dtb.Columns[i].ColumnName;
                    colQty.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
                    colQty.AppearanceHeader.ForeColor = Color.DarkRed;                    
                    this.gridBand_Qty.Columns.Add(colQty);
                }
                gridControl_ProducionOP.DataSource = dtb;
                bandedGridView_ProductionOP.BestFitColumns();
                view.ColumnPanelRowHeight = 30;
                view.BandPanelRowHeight = 20;                
                new BandedCellSelectionHelper(bandedGridView_ProductionOP);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Update Quantity]
        private void gridView_ProducionOP_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                GridView view = bandedGridView_ProductionOP as GridView;
                if (view.FocusedRowHandle >= 0)
                {
                    Line = Convert.ToInt16(view.GetFocusedDataRow()["Line"]);
                    groupControl_DailyInfo.Text = "Daily Info: " + Month + "-" + Year + ", " +
                                                   "PO: " + view.GetFocusedDataRow()["PO"] + ", " +
                                                   "Sub Item: " + view.GetFocusedDataRow()["SubItem"] + ", " +
                                                   "Sub Factory: " + view.GetFocusedDataRow()["SubFactoryName"] + ", " +
                                                   "Line: " + view.GetFocusedDataRow()["Line"];
                    string Days = GetDays(Year, Month);
                    dtb = cls.GetData("sp_ProductedQty", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line", "@Days" },
                                                         new object[] { Year.ToString(), Month.ToString(), OID.ToString(), SubIID.ToString(), SubFLID.ToString(), Line.ToString(), Days });
                    gridControl_DailInfo.DataSource = dtb;
                    gridView_DailInfo.BestFitColumns();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_DailInfo_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                string Datetemp = Year + "-" + Month + "-" + e.Column.FieldName;
                DateTime date = Convert.ToDateTime(Datetemp);
                int Qty = Convert.ToInt32(e.Value);
                cls.UpdateData("sp_ProductedQtyUpdate", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line", "@Date", "@Quantity" },
                                                        new object[] { Year, Month, OID, SubIID, SubFLID, Line, date, Qty });
                MessageBox.Show("Update Successfully!!");
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bandedGridView_ProductionOP_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                GridView view = bandedGridView_ProductionOP as GridView;
                if (view.FocusedRowHandle >= 0)
                {
                    Line = Convert.ToInt16(view.GetFocusedDataRow()["Line"]);
                    string Datetemp = Year + "-" + Month + "-" + e.Column.FieldName;
                    DateTime date = Convert.ToDateTime(Datetemp);
                    int Qty = Convert.ToInt32(e.Value);
                    cls.UpdateData("sp_ProductedQtyUpdate", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line", "@Date", "@Quantity" },
                                                            new object[] { Year, Month, OID, SubIID, SubFLID, Line, date, Qty });
                    MessageBox.Show("Update Successfully!!");
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion       

        #region[GetDays]
        private string GetDays(int year,int month)
        {
            int TotalDay;
            string Days=null;
            if (month == 2)
            {
                  if ((year%4 == 0 && year%100 !=0) || year%400 == 0)
                      TotalDay = 29;
                  else
                      TotalDay = 28;
            }
            else
            {
                if (month == 4 || month == 6 || month == 9 || month == 11)
                    TotalDay = 30;
                else
                    TotalDay = 31;
            }
            Days="[1]";
            for (int i = 2; i <= TotalDay; i++)
            {
                Days += ",[" + i + "]";
            }
           return Days;
        }
        #endregion       

        #region[Preview]
        public void Preview()
        {
            if (frmPV != null && !frmPV.IsDisposed)
            {
                frmPV.Select();
            }
            else
            {
                frmPV = new FrmPreView(bandedGridView_ProductionOP);                
                frmPV.LoadData();
                frmPV.Show();
            }
        }
        #endregion

        private void importWareHouseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ListItem.Clear();
                int intQty;
                double dblUnitPrice;
                GridView view = bandedGridView_ProductionOP as GridView;

                if ((Convert.IsDBNull(view.GetFocusedDataRow()["SalePrice"]) ? "" : view.GetFocusedDataRow()["SalePrice"].ToString()) != "")
                    dblUnitPrice = Convert.ToDouble(view.GetFocusedDataRow()["SalePrice"]);
                else
                    dblUnitPrice = 0;

                int[] intarLine = view.GetSelectedRows();

                foreach (int index in intarLine)
                {
                    strLine += view.GetDataRow(index)["Line"].ToString() + ",";
                }

                //Total Quantiry producted of Lines.
                DataTable dtbQty = cls.GetData("sp_ImportProductQuantity", new string[] { "@Year", "@Month", "@OID", "@SubIID", "@SubFLID", "@Line" }, new object[] { Year, Month, OID, SubIID, SubFLID, strLine });
                if (dtbQty.Rows.Count > 0)
                {
                    intQty = Convert.ToInt16(dtbQty.Rows[0]["ProductedQty"]);
                    ListItem.Add(OID);
                    ListItem.Add(intQty);
                    ListItem.Add(dblUnitPrice);

                    FrmImportProduct frmImptPro = new FrmImportProduct(-1) { MdiParent=ClassShare.frmain,Text="New Import Product"};
                    frmImptPro.LoadData();
                    frmImptPro.SetValue(ListItem);                   
                    frmImptPro.Show();  
                    frmImptPro.AddNew();
                    ClassShare.frmain.SelectedPageGroup("Import Product");
                    ClassShare.frmain.Savelayout(this.Text, "WareHouse Management", "Import Product");
                }
                else
                {
                    MessageBox.Show("Quantity is not null! Please check again!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}